function findMaxIdx(arr) {
  let idx = 0;
  let m = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > m) {
      m = arr[i];
      idx = i;
    }
  }
  return idx;
}
function inner(arr, n, res) {
  if (n > 1) {
    const idx = findMaxIdx(arr.slice(0, n))
    const before = Object.assign(arr)
    if (idx < n - 1) {
      const bottom = arr.slice(idx + 1, n)
      const top = arr.slice(0, idx + 1)
      top.reverse()
      arr = [...[...top, ...bottom].reverse(), ...arr.slice(n)]
      res.push({
        before,
        idx,
        n,
        after: Object.assign(arr)
      })
    }
    inner(arr, n - 1, res)
  } else {
    return
  }
}

export default function (arr) {
  const ret = []
  inner(arr, arr.length, ret)
  return ret
}

